package com.example.teaknow;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.teaknow.Adapter.NewsAdapter;
import com.example.teaknow.Adapter.teaAdapter;
import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Bean.TeaBean;
import com.example.teaknow.Dao.CollectionDao;
import com.example.teaknow.Dao.HistoryDao;
import com.example.teaknow.Dao.NewsDao;
import com.example.teaknow.Util.DataUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link KnowFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class KnowFragment extends Fragment {
    private ListView listView;
    private Boolean flag=false;
    private HistoryDao historyDao;
    private NewsDao newsDao;
    private ArrayList<TeaBean>teaBeans=new ArrayList<TeaBean>();
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public KnowFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment know.
     */
    // TODO: Rename and change types and number of parameters
    public static KnowFragment newInstance(String param1,String param2) {
        KnowFragment fragment = new KnowFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView=inflater.inflate(R.layout.fragment_know, container, false);
        listView=(ListView)rootView.findViewById(R.id.lv_teaType);
        initView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                newsDao=new NewsDao(getActivity());
                System.out.println(position);
                if(position==0){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new NewsAdapter(newsDao.queryNews("绿茶"),getActivity()));
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getContext(),NewsDetailActivity.class);
                            NewsBean newsBean=newsDao.queryNews("绿茶").get(position);

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                            Date date = new Date(System.currentTimeMillis());
                            newsBean.setTime(simpleDateFormat.format(date));
                            historyDao=new HistoryDao(getActivity());
                            historyDao.insertHistory(newsBean);

                            intent.putExtra("newsBean",newsBean);
                            startActivity(intent);
                        }
                    });
                }
                if(position==1){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new NewsAdapter(newsDao.queryNews("青茶"),getActivity()));
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getContext(),NewsDetailActivity.class);
                            NewsBean newsBean=newsDao.queryNews("青茶").get(position);
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                            Date date = new Date(System.currentTimeMillis());
                            newsBean.setTime(simpleDateFormat.format(date));
                            historyDao=new HistoryDao(getActivity());
                            historyDao.insertHistory(newsBean);
                            intent.putExtra("newsBean",newsBean);
                            startActivity(intent);
                        }
                    });
                }
                if(position==2){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new NewsAdapter(newsDao.queryNews("黄茶"),getActivity()));
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getContext(),NewsDetailActivity.class);
                            NewsBean newsBean=newsDao.queryNews("黄茶").get(position);
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                            Date date = new Date(System.currentTimeMillis());
                            newsBean.setTime(simpleDateFormat.format(date));
                            historyDao=new HistoryDao(getActivity());
                            historyDao.insertHistory(newsBean);
                            intent.putExtra("newsBean",newsBean);
                            startActivity(intent);
                        }
                    });
                }
                if(position==3){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new NewsAdapter(newsDao.queryNews("红茶"),getActivity()));
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getContext(),NewsDetailActivity.class);
                            NewsBean newsBean=newsDao.queryNews("红茶").get(position);

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                            Date date = new Date(System.currentTimeMillis());
                            newsBean.setTime(simpleDateFormat.format(date));
                            historyDao=new HistoryDao(getActivity());
                            historyDao.insertHistory(newsBean);

                            intent.putExtra("newsBean",newsBean);
                            startActivity(intent);
                        }
                    });
                }
                if(position==4){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new NewsAdapter(newsDao.queryNews("花茶"),getActivity()));
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getContext(),NewsDetailActivity.class);
                            NewsBean newsBean=newsDao.queryNews("花茶").get(position);

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                            Date date = new Date(System.currentTimeMillis());
                            newsBean.setTime(simpleDateFormat.format(date));
                            historyDao=new HistoryDao(getActivity());
                            historyDao.insertHistory(newsBean);

                            intent.putExtra("newsBean",newsBean);
                            startActivity(intent);
                        }
                    });
                }

            }
        });

        return rootView;
    }

    private void initView() {
        if(flag){

        }
        else {
            flag=true;
            teaBeans=DataUtil.initTeaType();
            for(int i=0;i<teaBeans.size();i++){
                System.out.println(teaBeans.get(i).getName());
            }
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listView.setAdapter(new teaAdapter(DataUtil.initTeaType(),getActivity()));
            }
        });

    }
}